package com.sdry.web.controller.zc.quality;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.Bom;
import com.sdry.model.hpf.MaterialComparison;
import com.sdry.model.jyy.Cut;
import com.sdry.model.workplace.Assemble;
import com.sdry.model.workplace.Centrifugal;
import com.sdry.model.zc.ZcEvaluateEntity;
import com.sdry.model.zc.ZcGasEntity;
import com.sdry.model.zc.ZcLeakHuntingEntity;
import com.sdry.model.zc.ZcProductTracingByOddNumberEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.service.hpf.PickApplyService;
import com.sdry.service.zc.ZcProductTracingService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcProductTracingController   
 * @Description:  产品追溯
 * @Author:       zc   
 * @CreateDate:   2018年11月23日 下午4:38:37   
 * @Version:      v1.0
 */
@RequestMapping("productTracing")
@Controller
public class ZcProductTracingController {

	@Resource
	private ZcProductTracingService zcProductTracingService;
	@Resource
	private PickApplyService pickApplyService;
	/**
	 * 进入追溯页面
	 * @return
	 */
	@RequestMapping("enterProductTracingPage")
	public String enterProductTracingPage(){
		return "/zc/quality/productTracing";
	}
	/**
	 * 通过产品条码查询装丝记录
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectZsInfoByCode")
	public void selectZsInfoByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		if(productCode == null){
			productCode="";
		}
		List<Assemble> selectZsInfoByCode = zcProductTracingService.selectZsInfoByCode(productCode);
		int count = zcProductTracingService.countZsInfoByCode(productCode);
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectZsInfoByCode, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		json.put("count", count);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过产品条码查询离心浇铸信息
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectJzInfoByCode")
	public void selectJzInfoByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		if(productCode == null){
			productCode="";
		}
		List<Centrifugal> selectJzInfoByCode = zcProductTracingService.selectJzInfoByCode(productCode);
		int count = zcProductTracingService.countJzInfoByCode(productCode);
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectJzInfoByCode, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		json.put("count", count);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过产品条码查询切割信息
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectCutInfoByCode")
	public void selectCutInfoByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		if(productCode == null){
			productCode="";
		}
		List<Cut> selectCutInfoByCode = zcProductTracingService.selectCutInfoByCode(productCode);
		int count = zcProductTracingService.countCutInfoByCode(productCode);
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectCutInfoByCode, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		json.put("count", count);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过产品条码查询检漏信息
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectLeakHuntingInfoByCode")
	public void selectLeakHuntingInfoByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		if(productCode == null){
			productCode="";
		}
		List<ZcLeakHuntingEntity> selectLeakHuntingInfoByCode = zcProductTracingService.selectLeakHuntingInfoByCode(productCode);
		int count = zcProductTracingService.countLeakHuntingInfoByCode(productCode);
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectLeakHuntingInfoByCode, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		json.put("count", count);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过产品条码查询历史检漏信息
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectLeakHuntingInfoLogByCode")
	public void selectLeakHuntingInfoLogByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		if(productCode == null){
			productCode="";
		}
		List<ZcLeakHuntingEntity> selectLeakHuntingInfoLogByCode = zcProductTracingService.selectLeakHuntingInfoLogByCode(productCode);
		int count = zcProductTracingService.countLeakHuntingInfoLogByCode(productCode);
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectLeakHuntingInfoLogByCode, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		json.put("count", count);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过产品条码查询组件评价信息
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectEvaluateInfoByCode")
	public void selectEvaluateInfoByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		if(productCode == null){
			productCode="";
		}
		List<ZcEvaluateEntity> selectEvaluateInfoByCode = zcProductTracingService.selectEvaluateInfoByCode(productCode);
		int count = zcProductTracingService.countEvaluateInfoByCode(productCode);
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectEvaluateInfoByCode, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		json.put("count", count);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过产品条码查询历史组件评价信息
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectEvaluateInfoLogByCode")
	public void selectEvaluateInfoLogByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		if(productCode == null){
			productCode="";
		}
		List<ZcEvaluateEntity> selectEvaluateInfoLogByCode = zcProductTracingService.selectEvaluateInfoLogByCode(productCode);
		int count = zcProductTracingService.countEvaluateInfoLogByCode(productCode);
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectEvaluateInfoLogByCode, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		json.put("count", count);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过产品条码查询组件评价信息
	 * @param request
	 * @param response
	 * @param code
	 * @throws Exception 
	 */
	@RequestMapping("selectEvaluateInfoByCode2")
	public void selectEvaluateInfoByCode2(HttpServletRequest request,HttpServletResponse response,String productCode) throws Exception{
		if(productCode == null){
			productCode="";
		}
		List<ZcEvaluateEntity> list = zcProductTracingService.selectEvaluateInfoByCode(productCode);
		JSONArray jsonArray = JSONArray.fromObject( list ); 
        ResponseUtil.write(response, jsonArray);
	}
	/**
	 * 通过产品条码查询气密性检测信息
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectGasInfoByCode")
	public void selectGasInfoByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		if(productCode == null){
			productCode="";
		}
		List<ZcGasEntity> selectGasInfoByCode = zcProductTracingService.selectGasInfoByCode(productCode);
		int count = zcProductTracingService.countGasInfoByCode(productCode);
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectGasInfoByCode, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		json.put("count", count);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过产品条码查询历史气密性检测信息
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectGasInfoLogByCode")
	public void selectGasInfoLogByCode(HttpServletRequest request,HttpServletResponse response,String productCode){
		if(productCode == null){
			productCode="";
		}
		List<ZcGasEntity> selectGasInfoLogByCode = zcProductTracingService.selectGasInfoLogByCode(productCode);
		int count = zcProductTracingService.countGasInfoLogByCode(productCode);
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(selectGasInfoLogByCode, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		json.put("count", count);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过排产单号追溯
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectProductTracingByOddNumber")
	public void selectProductTracingByOddNumber(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		ZcProductTracingByOddNumberEntity zcProductTracingByOddNumberEntity = zcProductTracingService.selectProductTracingByOddNumber(zcQueryCriteria);
		JSONObject json= JSONObject.fromObject(zcProductTracingByOddNumberEntity);  
		try {
			if(zcProductTracingByOddNumberEntity!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 通过排产单号查询bom偏差率
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectBomListRatio")
	public void selectBomListRatio(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria){
		List<MaterialComparison> bomList = pickApplyService.listAllMaterialComparison(zcQueryCriteria.getKeyword());
		JSONObject json = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		
		JSONArray jsonArray = JSONArray.fromObject(bomList, jsonConfig);
		json.put("code", 0);
		json.put("data", jsonArray);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
